data structures math matrices

Please click on ads to support us..

Python Code:

import sys
import heapq
from math import ceil, inf, gcd
from collections import defaultdict , Counter, deque
from bisect import bisect_left , bisect_right
import sys, threading
import random

mod = pow(10, 9) + 7

def ip():
  return int(input())

def input():
  return sys.stdin.readline().strip()

def readList():
  return list(map(int,input().split()))

def solve():
    n , m = readList()
    a = []
    b = []
    
    for _ in range(n):
        a.append(readList())
    
    for _ in range(n):
        b.append(readList())
    
    rows = defaultdict(int)
    columns = defaultdict(int)
    
    for i in range(n):
        for j in range(m):
            rows[b[i][j]] = i
            columns[b[i][j]] = j
    
    for i in range(n):
        val1 = rows[a[i][0]]
        for j in range(m):
            if rows[a[i][j]] != val1:
                print("NO")
                return
        
    
    for i in range(m):
        val1 = columns[a[0][i]]
        for j in range(n):
            if columns[a[j][i]] != val1:
                print("NO")
                return
    
    print("YES")
            
    
T = int(input())
for _ in range(T):
    solve()


Comments

Submit
0 Comments
More Questions

574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome